﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace TagBasedResourceManagePlatform.UI.Wpf.UIComponents
{
    /// <summary>
    /// ResourceListView.xaml 的交互逻辑
    /// </summary>
    public partial class ResourceListView : UserControl
    {
        #region Nested Class

        public class ItemCollection
        {
            private ResourceListView _view;

            internal ItemCollection(ResourceListView view)
            {
                _view = view;
            }

            public void Add(ResourceListViewItem item)
            {
                _view.Part_ItemsPanel.Children.Add(item);
            }

            public void Insert(int index, ResourceListViewItem item)
            {
                _view.Part_ItemsPanel.Children.Insert(index, item);
            }

            public void AddRange(IEnumerable<ResourceListViewItem> items)
            {
                foreach(var item in items)
                {
                    _view.Part_ItemsPanel.Children.Add(item);
                }
            }

            public ResourceListViewItem this[int index]
            {
                get { return (ResourceListViewItem) _view.Part_ItemsPanel.Children[index]; }
                set { _view.Part_ItemsPanel.Children[index] = value; }
            }

            public void Remove(ResourceListViewItem item)
            {
                _view.Part_ItemsPanel.Children.Remove(item);
            }

            public void RemoveAt(int index)
            {
                _view.Part_ItemsPanel.Children.RemoveAt(index);
            }

            public void RemoveRange(int index, int count)
            {
                _view.Part_ItemsPanel.Children.RemoveRange(index, count);
            }
        }

        #endregion

        #region Constants

        private const int PREFERED_ITEM_WIDTH = 200;

        #endregion

        #region Dependency Properties

        public static readonly DependencyProperty ActualItemWidthProperty = DependencyProperty.Register("ActualItemWidth", typeof(int), typeof(ResourceListView));

        #endregion

        #region Properties

        public int ActualItemWidth
        {
            get { return (int)GetValue(ActualItemWidthProperty); }
            set { SetValue(ActualItemWidthProperty, value); }
        }

        #endregion

        #region Overrided Functions

        #endregion

        #region Event Handlers

        private void Part_ItemsPanel_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            if (e.WidthChanged)
            {
                var count = Convert.ToInt32(Math.Floor(e.NewSize.Width / PREFERED_ITEM_WIDTH));
                if (count <= 0) count = 1;
                ActualItemWidth = Convert.ToInt32(Math.Floor(e.NewSize.Width / count));
            }
        }

        #endregion

        public ResourceListView()
        {
            InitializeComponent();

            Part_ItemsPanel.SizeChanged += Part_ItemsPanel_SizeChanged;
        }

    }
}
